/**
 * @file sun_time.cpp
 * @brief Brief description of sun_time.cpp
 * @author achdu0000@163.com
 * @version 1.0.0
 * @date 2024/07/07
 */

#include "sun_time.h"
#include <string.h>

// ==================================================
// =============== PRE-DECLARE ======================
// ==================================================

LV_IMG_DECLARE(img_sun_track_circle_200x74)
LV_IMG_DECLARE(img_icon_sun_20x20)
LV_IMG_DECLARE(img_icon_people_20x20)
LV_IMG_DECLARE(img_icon_twelve_circle_indic_5x5)

LV_FONT_DECLARE(lv_font_yahei_bold_12)

// ==================================================
// =============== DEFINATIONS ======================
// ==================================================

// ==================================================
// =============== STATIC VARIABLES =================
// ==================================================

static uint8_t sixty_jiazi_val;
static uint8_t birth_year_jiazi_val;

static uint16_t scale_width = 3;
static uint16_t scale_len = 25;
static lv_color_t scale_color = SCREEN_FG_COLOR;

// ==================================================
// =============== STATIC FUNCTION ==================
// ==================================================

// ==================================================
// =============== IMPLEMENTATION ===================
// ==================================================

bool set_now_year_ganzhi(const char *year_ganzhi_str)
{
    /* asset year_ganzhi_str in sixty jiazi strings */

    uint8_t i = 0;
    size_t len = (sizeof(sixty_jiazi_text_table) / sizeof(sixty_jiazi_text_table[0]));

    for (; i < len && strcmp(sixty_jiazi_text_table[i], year_ganzhi_str) != 0; i++)
    {
    }
    if (i == len)
        return false;
    sixty_jiazi_val = i;

    return true;
}

bool set_birth_year_jiazi_val(const char *birth_year_ganzhi_str)
{
    /* asset birth_year_ganzhi_str) in sixty jiazi strings */

    uint8_t i = 0;
    size_t len = (sizeof(sixty_jiazi_text_table) / sizeof(sixty_jiazi_text_table[0]));

    for (; i < len && strcmp(sixty_jiazi_text_table[i], birth_year_ganzhi_str) != 0; i++)
    {
    }

    if (i == len)
        return false;
    birth_year_jiazi_val = i;

    return true;
}

const char *get_now_year_ganzhi()
{
    return sixty_jiazi_text_table[sixty_jiazi_val];
}

bool create_basic_sixty_jiazi_clock_ui(lv_obj_t *parent, lv_coord_t width, lv_coord_t height, lv_coord_t *content_w_ptr, lv_coord_t *content_h_ptr)
{
    lv_obj_t *indic_bg_boarder = lv_obj_create(parent);
    lv_coord_t bg_boader_width = scale_len + 2;
    lv_obj_set_size(indic_bg_boarder, width, height);
    lv_obj_set_style_bg_opa(indic_bg_boarder, LV_OPA_TRANSP, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_radius(indic_bg_boarder, LV_RADIUS_CIRCLE, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(indic_bg_boarder, bg_boader_width, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_color(indic_bg_boarder, SCREEN_SIXTY_JIAZI_CIRCLE_BG_COLOR, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_center(indic_bg_boarder);

    lv_obj_t *meter = lv_meter_create(parent);
    lv_obj_set_size(meter, width, height);
    lv_obj_set_style_bg_opa(meter, LV_OPA_TRANSP, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_center(meter);

    /*Remove the circle from the middle of `meter`*/
    lv_obj_remove_style(meter, nullptr, LV_PART_INDICATOR);
    /*Remove the padding from `meter` (especially the padding between ticks and borader)*/
    lv_obj_set_style_pad_all(meter, 0, LV_PART_MAIN | LV_STATE_DEFAULT);

    lv_meter_scale_t *scale = lv_meter_add_scale(meter);
    lv_meter_set_scale_ticks(meter, scale, 61, scale_width, scale_len, scale_color);
    lv_meter_set_scale_range(meter, scale, 0, 60, 360, 267);

    lv_meter_indicator_t *arc_indic = lv_meter_add_arc(meter, scale, scale_len, get_wuxing_color(sixty_jiazi_wuxing_table[sixty_jiazi_val]), 0);
    lv_meter_set_indicator_start_value(meter, arc_indic, sixty_jiazi_val);
    lv_meter_set_indicator_end_value(meter, arc_indic, sixty_jiazi_val + 1);

    *content_w_ptr = width - 2 * bg_boader_width;
    *content_h_ptr = height - 2 * bg_boader_width;

    return true;
}

bool create_extend_1_sun_track_and_sun_ui(lv_obj_t *parent, lv_coord_t x_ofs, lv_coord_t y_ofs)
{
    lv_obj_t *img = lv_img_create(parent);
    lv_img_set_src(img, &img_sun_track_circle_200x74);
    lv_img_set_angle(img, 3550);
    lv_obj_align_to(img, parent, LV_ALIGN_CENTER, x_ofs, y_ofs);

    lv_obj_t *sun_img = lv_img_create(parent);
    lv_img_set_src(sun_img, &img_icon_sun_20x20);
    lv_obj_align_to(sun_img, img, LV_ALIGN_BOTTOM_LEFT, -5, 10);

    return true;
}

bool create_extend_1_sun_time_connect_people_ui(lv_obj_t *parent, lv_coord_t border_radius)
{
    /* draw [indicator arc] to show the path from the birth year to now year */
    lv_obj_t *arc = lv_arc_create(parent);
    lv_obj_set_size(arc, border_radius * 2, border_radius * 2);
    uint16_t angle_ofs = 3;
    lv_arc_set_rotation(arc, 270 + birth_year_jiazi_val * 6 - angle_ofs);
    lv_arc_set_bg_angles(arc, 0, 360);
    lv_arc_set_start_angle(arc, 0);
    uint16_t end_val = (sixty_jiazi_val > birth_year_jiazi_val ? (sixty_jiazi_val - birth_year_jiazi_val) : (sixty_jiazi_val - birth_year_jiazi_val + 60));
    lv_arc_set_end_angle(arc, end_val * 6);

    lv_obj_set_style_pad_all(arc, 2, LV_PART_MAIN);
    lv_obj_set_style_arc_opa(arc, LV_OPA_TRANSP, LV_PART_MAIN);
    lv_obj_set_style_arc_opa(arc, LV_OPA_50, LV_PART_INDICATOR);
    lv_obj_set_style_arc_rounded(arc, false, LV_PART_INDICATOR);
    lv_obj_set_style_arc_width(arc, 24, LV_PART_INDICATOR);

    /* be sure the knob is not displayed */
    lv_obj_remove_style(arc, nullptr, LV_PART_KNOB);
    // lv_obj_set_style_arc_opa(arc, LV_OPA_TRANSP, LV_PART_KNOB);
    /* do not allow adjusting by click */
    lv_obj_clear_flag(arc, LV_OBJ_FLAG_CLICKABLE);

    lv_obj_center(arc);

    /* draw [icon people] to show the jiazi of birth year */
    lv_coord_t r = LV_MAX(border_radius - 15, 1);
    lv_obj_t *img_people = lv_img_create(parent);
    lv_img_set_src(img_people, &img_icon_people_20x20);

    lv_coord_t angle = birth_year_jiazi_val * 6;
    lv_img_set_angle(img_people, angle * 10);
    angle -= 2;
    lv_coord_t x_ofs = r * lv_trigo_cos(90 - angle) / LV_TRIGO_SIN_MAX;
    lv_coord_t y_ofs = -r * lv_trigo_sin(90 - angle) / LV_TRIGO_SIN_MAX;
    lv_obj_align_to(img_people, parent, LV_ALIGN_CENTER, x_ofs, y_ofs);

    /* draw [icon sun] */
    lv_obj_t *img_sun = lv_img_create(parent);
    lv_img_set_src(img_sun, &img_icon_sun_20x20);

    angle = sixty_jiazi_val * 6;
    lv_img_set_angle(img_sun, angle * 10);
    angle -= 4;
    x_ofs = r * lv_trigo_cos(90 - angle) / LV_TRIGO_SIN_MAX;
    y_ofs = -r * lv_trigo_sin(90 - angle) / LV_TRIGO_SIN_MAX;
    lv_obj_align_to(img_sun, parent, LV_ALIGN_CENTER, x_ofs, y_ofs);

    /* draw [indicator tri] to show every 12 year circle*/
    r = LV_MAX(border_radius - 5, 1);
    for (uint8_t i = 0; i < 6; i++)
    {
        lv_obj_t *img_indic = lv_img_create(parent);
        lv_img_set_src(img_indic, &img_icon_twelve_circle_indic_5x5);
        lv_img_set_zoom(img_indic, 256 | 128);
        angle = (birth_year_jiazi_val + i * 12) * 6;
        lv_img_set_angle(img_indic, angle * 10);
        angle += 2;
        x_ofs = r * lv_trigo_cos(90 - angle) / LV_TRIGO_SIN_MAX;
        y_ofs = -r * lv_trigo_sin(90 - angle) / LV_TRIGO_SIN_MAX;
        lv_obj_align_to(img_indic, parent, LV_ALIGN_CENTER, x_ofs, y_ofs);

        img_indic = lv_img_create(parent);
        lv_img_set_src(img_indic, &img_icon_twelve_circle_indic_5x5);
        lv_img_set_zoom(img_indic, 256 | 128);
        lv_img_set_angle(img_indic, angle * 10);
        angle -= 4;
        x_ofs = r * lv_trigo_cos(90 - angle) / LV_TRIGO_SIN_MAX;
        y_ofs = -r * lv_trigo_sin(90 - angle) / LV_TRIGO_SIN_MAX;
        lv_obj_align_to(img_indic, parent, LV_ALIGN_CENTER, x_ofs, y_ofs);
    }

    return true;
}

bool create_extend_2_year_ganzhi_label_ui(lv_obj_t *parent, lv_coord_t x_ofs, lv_coord_t y_ofs)
{
    lv_obj_t *label = lv_label_create(parent);
    lv_label_set_text_fmt(label, "【天时-年】\n%s", get_now_year_ganzhi());
    lv_obj_set_style_text_font(label, &lv_font_yahei_bold_12, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(label, lv_color_make(112, 48, 160), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_align_to(label, parent, LV_ALIGN_CENTER, x_ofs, y_ofs);
    return true;
}